//
// Copyright © 2021 dev. All rights reserved.
// 

import Cocoa
import os.log
import RxSwift
import RealmSwift
import ConfCore

final class AppCoordinator {
    let log = OSLog(subsystem: "WWDC", category: "AppCoordinator")
    private let disposeBag = DisposeBag()
    var liveObserver:LiveObserver?
     
    var storage:Storage
    var syncEngine:SyncEngine
    var windowController:MainWindowController
    var tabController:WWDCTabViewController<MainWindowTab>

    var featuredController: FeaturedContentViewController
    var scheduleController: ScheduleContainerViewController
    var videosController: SessionsSplitViewController
    var communityController: CommunityViewController

    var currentPlayerController: VideoPlayerViewController?
    var currentActivity: NSUserActivity?

    var activeTab: MainWindowTab = .schedule
    var playerOwnerTab:MainWindowTab?
    var playerOwnerSessionIdentifier:String? {
        didSet {
            rxPlayerOwnerSessionIdentifier.onNext(playerOwnerSessionIdentifier)
        }
    }
    var rxPlayerOwnerSessionIdentifier = BehaviorSubject<String?>(value: nil)

var isTransitioningPlayerContext = false

    var wasPlayingWhenClipSharingBegan = false


    init(windowController: MainWindowController, storage: Storage, syncEngine: SyncEngine) {
        self.storage = storage
        self.syncEngine = syncEngine
//        windowController.titleBarViewController.statusViewController = Downlo
        liveObserver = LiveObserver(dateProvider: today, storage: storage, syncEngine: syncEngine)

//        Primary UI Initialization
        tabController = WWDCTabViewController(windowController: windowController)

        // Featured
        featuredController = FeaturedContentViewController()
        featuredController.identifier = NSUserInterfaceItemIdentifier(rawValue: "Featured")
        let featuredItem = NSTabViewItem(viewController: featuredController)
        featuredItem.label = "Featured"
        tabController.addTabViewItem(featuredItem)

        // Schedule
        scheduleController = ScheduleContainerViewController(windowController: windowController, listStyle: .schedule)
        scheduleController.identifier = NSUserInterfaceItemIdentifier(rawValue: "Schedule")
        scheduleController.splitViewController.splitView.identifier = NSUserInterfaceItemIdentifier(rawValue: "ScheduleSplitView")
        scheduleController.splitViewController.splitView.autosaveName = "ScheduleSplitView"
        let scheduleItem = NSTabViewItem(viewController: scheduleController)
        scheduleItem.label = "Schedule"
        scheduleItem.initialFirstResponder = scheduleController.splitViewController.listViewController.tableView
        tabController.addTabViewItem(scheduleItem)

        // Videos
        videosController = SessionsSplitViewController(windowController: windowController, listStyle: .videos)
        videosController.identifier = NSUserInterfaceItemIdentifier(rawValue: "Videos")
        videosController.splitView.identifier = NSUserInterfaceItemIdentifier(rawValue: "VideosSplitView")
        videosController.splitView.autosaveName = "VideosSplitView"
        let videosItem = NSTabViewItem(viewController: videosController)
        videosItem.label = "Videos"
        videosItem.initialFirstResponder = videosController.listViewController.tableView
        tabController.addTabViewItem(videosItem)

    }
}
